﻿// Inner Fire 游戏引擎库
// DxPostProcessing 渲染器初始化功能
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 用于DirectX的渲染器。
// 此文件分装用于初始化功能
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-04-23

#include "dx_post_processing.h"
#include "dx_graphics.h"

namespace ifire::dx {
ifire::dx::DxPostProcessing::DxPostProcessing(DxGraphics* dx) : dx_(dx) {}
void DxPostProcessing::InitConfig() {
  auto& cfg = dx_->Config()["PostProcessing"];
  sobel_enable_ = cfg("SobelEnable", false);
}

void DxPostProcessing::Init() {
  // SSAO贴图
  ssao_ = std::make_unique<Ssao>(
      dx_->Device(), dx_->CommandList(), dx_->GetWidth(), dx_->GetHeight());
  // 用于索贝尔算子
  sobel_filter_ = std::make_unique<SobelFilter>(
      dx_->Device(), dx_->GetWidth(), dx_->GetHeight(), BACK_BUFFER_FORMAT);
  // 用于模糊
  offscreen_rt_ = std::make_unique<RenderTarget>(
      dx_->Device(), dx_->GetWidth(), dx_->GetHeight(), BACK_BUFFER_FORMAT);
}

void DxPostProcessing::Reset() {
  // if (shadow_map_ != nullptr) {
  //   // 这东西非常奇怪。我不执行这个Resize，反而结果是正确的！
  //   // mShadowMap->OnResize(GetWidth(), GetHeight());
  // }
  if (ssao_ != nullptr) {
    ssao_->OnResize(dx_->GetWidth(), dx_->GetHeight());
    ssao_->RebuildDescriptors(dx_->GetSwapchain().GetDepthStencil());
  }

  if (sobel_filter_ != nullptr) {
    sobel_filter_->OnResize(dx_->GetWidth(), dx_->GetHeight());
  }
  if (offscreen_rt_ != nullptr) {
    offscreen_rt_->OnResize(dx_->GetWidth(), dx_->GetHeight());
  }
}

} // namespace ifire::dx